package com.weipu.common.facade.support.service.task;

import java.util.ArrayList;

import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.CacheUtil;
import com.weipu.common.util.GlobalConfig;
import com.weipu.common.util.Logger;

/**
 *根据类目，相应查询类型(如清仓等)查询本地mongodb产品数据集
* @param startindex M 起始位置
* @param pageCount M 每页条数
* @param orderByField O sort排序
* @param startPrice M 起始价格： 默认为0
* @param endPrice  M 结束价格：默认为无穷大
* @param beginStar M 多少评分等级以上 默认为0
* @param relatedType  M 查询类型
* @param isLoadFromCache  M 是否支持缓存读取
* @author Administrator
*/
public class GetProductListByCategoryIdAndRelatedTypeTask extends BaseServiceTask<ArrayList<ProductModel>>
{
    private int startindex;
    
    private int pageCount;
    
    private String orderByField;
    
    private double startPrice;
    
    private double endPrice;
    
    private double beginStar;
    
    private String relatedType;
    
    private boolean isLoadFromCache;
    
    public GetProductListByCategoryIdAndRelatedTypeTask(int startindex, int pageCount, String orderByField,
        double startPrice, double endPrice, double beginStar, String relatedType, boolean isLoadFromCache)
    {
        this.startindex = startindex;
        this.pageCount = pageCount;
        this.orderByField = orderByField;
        this.startPrice = startPrice;
        this.endPrice = endPrice;
        this.beginStar = beginStar;
        this.relatedType = relatedType;
        this.isLoadFromCache = isLoadFromCache;
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 8591155121990184876L;
    
    public boolean executeRequest()
    {
        return true;
    }
    
    public ArrayList<ProductModel> execute()
    {
        ArrayList<ProductModel> resultList = null;
        try
        {
            resultList =
                ProviderFactory.createProductProvider().getProductListByCategoryIdAndRelatedType(startindex,
                    pageCount,
                    orderByField,
                    startPrice,
                    endPrice,
                    beginStar,
                    relatedType,
                    isLoadFromCache);
        }
        catch (Exception e)
        {
            Logger.w(TAG, "getProductProvider().getTopCategories(false) fail.", e);
        }
        
        return resultList;
    }
    
    @Override
    protected void executeResponse(ArrayList<ProductModel> resultObj)
    {
        //保存至本地
        cacheData(resultObj);
        
        //上报公共处理模块
        super.executeResponse(resultObj);
        
    };
    
    private void cacheData(ArrayList<ProductModel> result)
    {
        //保存本地数据:内存缓存和本地数据库缓存
        if (null != result && !result.isEmpty())
        {
            //刷新内存缓存(如果开启的话)
            if (CacheUtil.getInstance().isEnableProductMemCache())
            {
                GlobalConfig.getInstance().getTopProductCache().put(relatedType, new ArrayList<ProductModel>(result));
            }
            
            //刷新本地数据库缓存
            if (CacheUtil.getInstance().isEnableLocaleDBCache())
            {
                //刷新本地数据库
                ProviderFactory.createProductDBProvider().deleteProductListByType(relatedType);
                
                for (int i = 0; i < result.size(); i++)
                {
                    ProviderFactory.createProductDBProvider().saveProductInfo(result.get(i), relatedType);
                }
            }
        }
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof GetProductListByCategoryIdAndRelatedTypeTask))
        {
            return false;
        }
        
        //        this.startindex = startindex;
        //        this.pageCount = pageCount;
        //        this.orderByField = orderByField;
        //        this.startPrice = startPrice;
        //        this.endPrice = endPrice;
        //        this.beginStar = beginStar;
        //        this.relatedType = relatedType;
        //        this.isLoadFromCache = isLoadFromCache;
        
        return this.toString().equals(o.toString());
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    @Override
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("startindex: ").append(startindex).append(',');
        sb.append("pageCount: ").append(pageCount).append(',');
        sb.append("orderByField: ").append(orderByField).append(',');
        sb.append("endPrice: ").append(endPrice).append(',');
        sb.append("beginStar: ").append(beginStar).append(',');
        sb.append("relatedType: ").append(relatedType).append(',');
        sb.append("startindex: ").append(startindex).append(',');
        sb.append("isLoadFromCache: ").append(isLoadFromCache);
        return sb.toString();
    }
}
